public class sadf2 {
    class Solution {
        public String tree2str(TreeNode root) {
            StringBuilder stringbuilder = new StringBuilder();
            strmethod(root,stringbuilder);
            return stringbuilder.toString();
        }
        public void strmethod(TreeNode root,StringBuilder stringbuilder){
            if(root==null){
                return;
            }
            stringbuilder.append(root.val);
            if(root.left != null){
                stringbuilder.append("(");
                strmethod(root.left,stringbuilder);
                stringbuilder.append(")");
            }
            else{
                if(root.right == null){
                    return;
                }else{
                    stringbuilder.append("()");
                }
            }
            if(root.right != null){
                stringbuilder.append("(");
                strmethod(root.right,stringbuilder);
                stringbuilder.append(")");
            }
            else{
                return;
            }
        }
    }
}
